<div id="header"><p>Your Room<a href="?p=login&action=logout">logout</a></p></div>
<?php

	// If there is a database error, inform the user
	if($database->errno != 0)
	{
		echo '<div id="subheader">Database Error</div>';
		echo "<p> There is a database connection error.  We apologize for the inconvenience. Please try again later.</p>";
		return;
	}
	// Establish colors for the
	$tempColors = get_temp_colors();

	// Get all active sensors
	$sensors = get_active_sensors($database);

	// Go through each sensor...
	foreach($sensors as $id=>$name)
	{
		
		// Get latest data of sensor
		$results = $database->multi_query("CALL getLatestDataOfSensor($id,1)");

		if($results)
		{

			$results = $database->use_result();

			while($row = $results->fetch_row())
			{
				// Assign a temperature color
				$tempColor = assign_temp_color($tempColors,(float)$row[4]);

				// Create html for this data
				$output .= '<div id="subheader" style="background-color:'.$tempColor.'">'.$name.'</div>';
				$output .= '<div id="currentdata">';
				$output .= '<div id="temperature" style="color:'.$tempColor.'">';
				$output .= '<p>'.$row[4].'&#176</p></div>';

				// Depending on light state, determine which lightbulb icon to use
				if($row[5] == "on")
				{
					$output .= '<div id="light">
											<img src="../icons/lightbulb.png" title="The Light is On"/>
											</div>';
				}
				else
				{
					$output .= '<div id="light">
											<img src="../icons/lightbulb_off.png" title="The Light is Off"/>
											</div>';
				}

				$output .=  '<div id="door">Door: '.$row[6].'</div>';
				$output .=  "</div>";
			}
			if($database->more_results())
  			while($database->next_result());
		}

	}

	// Echo out the html
	echo $output;
?>
